
/* components */
import PlanTimeDialog from '@src/modules/task/view/components/PlanTimeDialog.vue'
import CalendarPlanTimeDialog from '@src/modules/task/view/components/CalendarPlanTimeDialog.vue'
/* enum */
import ComponentNameEnum from '@model/enum/ComponentNameEnum'
/* entity */
import Field from '@model/entity/Field'
/* vue */
import VC from '@model/VC'
import { Component, Emit, Prop, Ref } from 'vue-property-decorator'
import { CreateElement } from 'vue'
/* util */
import Log from '@src/util/log.ts'
import Platform from '@src/util/platform'
import { isCalendar } from '@src/util/CalendarUtil';
/* scss */
import '@src/component/business/BizModifyPlanTime/BizModifyPlanTime.scss'
/* service */
import { allowModifyPlanTimeJudgeHandler } from '@service/TaskService'
import { t } from '@src/locales'

enum BizModifyPlanTimeEmitEventNameEnum {
  Success = 'success'
}

@Component({ 
  name: ComponentNameEnum.BizModifyPlanTime,
  components: {
    [PlanTimeDialog.name]: PlanTimeDialog,
    [CalendarPlanTimeDialog.name]: CalendarPlanTimeDialog
  }
})
export default class BizModifyPlanTime extends VC {
  /* 计划时间弹窗 */
  @Ref() readonly PlanTimeDialogComponent !: PlanTimeDialog
  @Ref() readonly CalendarPlanTimeDialogComponent !: CalendarPlanTimeDialog
  
  /* 是否允许发送短信 */
  @Prop({ default: true }) readonly enableSendSms: boolean | undefined
  /* 是否修改计划时间 */
  @Prop({ default: true }) readonly modifiable: boolean | undefined
  /* 工单信息 */
  @Prop() readonly task: any | undefined
  /* 计划时间字段 */
  @Prop() readonly field: Field | undefined
  
  /** 
   * @description 显示修改计划时间弹窗
  */
  public showModifyPlanTimeDialog() {
    Log.succ(Log.Start, this.showModifyPlanTimeDialog.name)
    // @ts-ignore
    isCalendar ? this.CalendarPlanTimeDialogComponent?.openDialog('modifyPlanTime') : this.PlanTimeDialogComponent?.openDialog();
  }
  
  /** 
   * @description 修改计划时间完成回调
  */
  @Emit(BizModifyPlanTimeEmitEventNameEnum.Success)
  private modifyPlanTimeSuccessCallBack(planTime: string) {
    Log.succ(Log.Start, this.modifyPlanTimeSuccessCallBack.name)
    return planTime
  }

  /** 
   * @description 修改计划时间完成回调
  */
  @Emit(BizModifyPlanTimeEmitEventNameEnum.Success)
  private modifyCalendarPlanTimeSuccessCallBack(newCalendarTime: object) {
    Log.succ(Log.Start, this.modifyCalendarPlanTimeSuccessCallBack.name)
    return newCalendarTime;
  }
  
  render(h: CreateElement) {
    return (
      <div class={ComponentNameEnum.BizModifyPlanTime}>
        <el-tooltip class='item' effect='dark' content={t('task.modifyPlanTime')} placement='top'>
          <i class='iconfont icon-bianji1 link-text' onClick={() => this.showModifyPlanTimeDialog()}></i>
        </el-tooltip>
        { this.field && <plantime-dialog
            appendToBody={true}
            ref='PlanTimeDialogComponent' 
            enableSendSms={this.enableSendSms}
            field={this.field}
            modifiable={this.modifiable}
            top
            task={this.task}
            unique={false}
            successCallback={(planTime: string) => this.modifyPlanTimeSuccessCallBack(planTime)}
          />
        }
        {
          isCalendar && <calendar-plantime-dialog
            appendToBody={true}
            ref='CalendarPlanTimeDialogComponent'
            enableSendSms={this.enableSendSms}
            task={this.task}
            modifiable={allowModifyPlanTimeJudgeHandler(this.task)}
            isAcceptCalendarTime={false}
            successCallback={(newCalendarTime: object) => this.modifyCalendarPlanTimeSuccessCallBack(newCalendarTime)} 
          />
        }
      </div>
    )
  }
}

